package com.faintlight.controller;

import com.faintlight.model.Sentence;
import com.faintlight.service.SentenceService;
import com.faintlight.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/sentences")
@Tag(name = "暖意罐", description = "温暖句子管理接口")
@RequiredArgsConstructor
public class SentenceController {

    private static final Logger logger = LoggerFactory.getLogger(SentenceController.class);

    private final SentenceService sentenceService;
    private final UserService userService;

    @PostMapping
    @Operation(summary = "收藏新句子")
    public ResponseEntity<Sentence> saveSentence(@RequestBody Sentence sentence, @RequestHeader("X-User-Id") Long userId) {
        sentence.setCollectedBy(userService.getUserById(userId));
        Sentence saved = sentenceService.saveSentence(sentence);
        return ResponseEntity.ok(saved);
    }

    @GetMapping("/user/{userId}")
    @Operation(summary = "获取用户收藏的句子")
    public ResponseEntity<List<Sentence>> getUserSentences(@PathVariable Long userId) {
        List<Sentence> sentences = sentenceService.getUserSentences(userId);
        return ResponseEntity.ok(sentences);
    }

    @GetMapping("/recommendations")
    @Operation(summary = "获取系统推荐句子")
    public ResponseEntity<List<Sentence>> getSystemRecommendations() {
        List<Sentence> sentences = sentenceService.getSystemRecommendations();
        return ResponseEntity.ok(sentences);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除收藏句子")
    public ResponseEntity<Map<String, String>> deleteSentence(@PathVariable Long id,
                                                              @RequestHeader("X-User-Id") Long userId) {
        sentenceService.deleteSentence(id, userId);

        Map<String, String> response = new HashMap<>();
        response.put("message", "句子删除成功");
        return ResponseEntity.ok(response);
    }
    @GetMapping("/user/{userId}/count")
    @Operation(summary = "获取用户收藏句子数量")
    public ResponseEntity<Long> getUserSentenceCount(@PathVariable Long userId) {
        Long count = sentenceService.getUserSentenceCount(userId);
        return ResponseEntity.ok(count);
    }
}
